package com.europe.business.europebusiness.ui.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.europe.business.europebusiness.R;
import com.europe.business.europebusiness.ui.adapter.SectionAdapter;
import com.europe.business.europebusiness.ui.bean.Section;
import com.europe.business.europebusiness.ui.fragment.base.ActionBarFragment;
import com.europe.business.europebusiness.ui.fragment.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

public class EuFragment extends ActionBarFragment implements View.OnClickListener, SectionAdapter.OnSectionClickListener,View.OnTouchListener {

    private LinearLayout mLinIndustry;
    private LinearLayout mLinArea;
    private LinearLayout mLinType;
    private LinearLayout mLinScope;

    private TextView mIndustry;
    private TextView mArea;
    private TextView mType;
    private TextView mScope;

    private LinearLayout mFilter;
    private LinearLayout root;

    PopupWindow mPopupWindow;
    RecyclerView section2;
    RecyclerView section1;
    List<Section> list;
    List<Section> list2;
    SectionAdapter sectionAdapter1;
    SectionAdapter sectionAdapter2;
    private View line;
    private ImageView industryDown;

    @Override
    protected int setMainContent() {
        return R.layout.fragment_eu;
    }

    public static Fragment newInstance() {
        EuFragment euFragment = new EuFragment();
        return euFragment;
    }

    @Override
    public void onViewCreated(@android.support.annotation.NonNull View view, @android.support.annotation.Nullable android.os.Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setToolbarVisible(View.VISIBLE);
        setTitle("欧企");
        setToolBarLeftImage(View.VISIBLE, R.drawable.msg_black);
        setToolBarRightImage(View.VISIBLE, R.drawable.search_black);
        setToolBarRightText(View.GONE, "");

    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mLinIndustry = view.findViewById(R.id.lin_industry);
        mLinArea = view.findViewById(R.id.lin_area);
        mLinType = view.findViewById(R.id.lin_type);
        mLinScope = view.findViewById(R.id.lin_scope);
        mFilter = view.findViewById(R.id.filter);
        root = view.findViewById(R.id.root);
        line = view.findViewById(R.id.line);

        mIndustry = view.findViewById(R.id.industry);
        industryDown = view.findViewById(R.id.industruy_down);
        mArea = view.findViewById(R.id.area);
        mType = view.findViewById(R.id.type);
        mScope = view.findViewById(R.id.scope);

        mLinIndustry.setOnTouchListener(this);
        mLinArea.setOnTouchListener(this);
        mLinType.setOnTouchListener(this);
        mLinScope.setOnTouchListener(this);
        initPopWindow();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
        showToastShort("-isSelected->" + mIndustry.isSelected());
    }


    private void startAnim() {
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.section);
        animation.setFillAfter(true);
        industryDown.startAnimation(animation);
    }

    private void clearAnim() {
        industryDown.clearAnimation();
    }

    private void initPopWindow() {
        int height = (int) (((Activity) context).getWindowManager().getDefaultDisplay().getHeight() * 0.6);
        int width = (int) (((Activity) context).getWindowManager().getDefaultDisplay().getWidth() * 0.5);
        View view = LayoutInflater.from(context).inflate(R.layout.pop_section, null, false);
        section1 = view.findViewById(R.id.section1);
        section2 = view.findViewById(R.id.section2);


        list = new ArrayList<Section>();//总list
        //--------------------------第一个数据---------------------------
        Section section = new Section();
        section.setSectionId(1);
        section.setSectionName("不限");

        List<Section> list2 = new ArrayList<Section>();
        Section section22 = new Section();
        section22.setSectionId(11);
        section22.setSectionName("农业11");
        list2.add(section22);

        section22 = new Section();
        section22.setSectionId(111);
        section22.setSectionName("农业111");
        list2.add(section22);

        section.setSection2Id(list2);
        list.add(section);
        //--------------------------第二个数据---------------------------
        section = new Section();
        section.setSectionId(2);
        section.setSectionName("畜牧业");
        List<Section> list22 = new ArrayList<Section>();
        Section section33 = new Section();
        section33.setSectionId(22);
        section33.setSectionName("畜牧业22");
        list22.add(section33);
        section33 = new Section();
        section33.setSectionId(222);
        section33.setSectionName("畜牧业222");
        list22.add(section33);
        section.setSection2Id(list22);
        list.add(section);


        sectionAdapter1 = new SectionAdapter(context, list, 1);
        sectionAdapter1.setOnSectionClickListener(this);
        section1.setLayoutManager(new LinearLayoutManager(context));
        section1.setAdapter(sectionAdapter1);
        section1.setLayoutParams(new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT));

        mPopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, height);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                clearAnim();
            }
        });

    }

    private void notifyDataChanded() {
        if (null != sectionAdapter1) {
            sectionAdapter1.notifyDataSetChanged();
        }
        if (null != sectionAdapter2) {
            sectionAdapter2.notifyDataSetChanged();
        }

    }

    private void showOrDismissPopWindow() {
        showToastShort("showPopWindow");
        if (mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        } else {
            mPopupWindow.showAsDropDown(line);
            notifyDataChanded();
        }
    }

    @Override
    public void onSectionClick(int id, int flag) {

        if (flag == 1) {//表示点击的是一级菜单
            for (Section section : list) {
                if (section.getSectionId() == id) {
                    list2 = section.getSection2Id();
                }
            }

            int width = (int) (((Activity) context).getWindowManager().getDefaultDisplay().getWidth() * 0.5);
            section2.setVisibility(View.VISIBLE);
            section2.setLayoutParams(new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT));
            List<Section> list = new ArrayList<Section>();

            sectionAdapter2 = new SectionAdapter(context, list2, 2);
            sectionAdapter2.setOnSectionClickListener(this);
            section2.setLayoutManager(new LinearLayoutManager(context));
            section2.setAdapter(sectionAdapter2);
        } else {
            String name = "";
            for (Section section : list2) {
                if (section.getSectionId() == id) {
                    name = section.getSectionName();
                }
            }
            mIndustry.setText(name);
            showOrDismissPopWindow();
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            switch (v.getId()) {
                case R.id.lin_industry:
                    showOrDismissPopWindow();
                    startAnim();
                    break;
                case R.id.lin_area:
                    break;
                case R.id.lin_type:
                    break;
                case R.id.lin_scope:
                    break;
            }
        }
        return false;
    }
}
